package com.example.neusoft.cook.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.cook.MainActivity;
import com.example.neusoft.cook.R;
import com.example.neusoft.cook.base.BaseAcyivity;
import com.example.neusoft.cook.bean.SampleLogin;
import com.example.neusoft.cook.bean.SampleRegister;
import com.example.neusoft.cook.interf.Config;
import com.example.neusoft.cook.interf.RetrofitService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RegsiterActivity extends BaseAcyivity {
    private EditText userName;
    private  EditText password1R;
    private  EditText password2R;
    private  EditText telphone;
    private  EditText comments;
    private  EditText addressR;

    private Button  regR;
    private  Button back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regsiter);

        initView();
        setListener();
    }

    private  void setListener(){

        regR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //2、提交注册信息
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Config.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RetrofitService retrofitService = retrofit.create(RetrofitService.class);

                final String username = userName.getText().toString();
                String password = password1R.getText().toString();
                String password2 = password2R.getText().toString();
                String mobilenum = telphone.getText().toString();
                String address = addressR.getText().toString();
                String comment = comments.getText().toString();



                if (!password.equals("")&&password.equals(password2)){

                    Call<SampleRegister> regCall = retrofitService.userRegister(username,password,mobilenum,address,comment);
                    regCall.enqueue(new Callback<SampleRegister>() {
                        @Override
                        public void onResponse(Call<SampleRegister> call, Response<SampleRegister> response) {
                            SampleRegister r =response.body();
                            if (r.getSuccess().equals("0")){
                                Toast.makeText(RegsiterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                            }else {

                                Intent data = new Intent();
                                data.putExtra("username",username);
                                setResult(RESULT_OK,data);
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(Call<SampleRegister> call, Throwable t) {

                        }
                    });

                }else {
                    Toast.makeText(RegsiterActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                }

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();

            }
        });

    }

    private  void initView(){
        userName = (EditText) findViewById(R.id.userName);
        password1R= (EditText) findViewById(R.id.userpassword);
        password2R = (EditText) findViewById(R.id.passwordL);
        telphone = (EditText) findViewById(R.id.tel);
        comments = (EditText) findViewById(R.id.comment);
        addressR = (EditText) findViewById(R.id.address);

        regR = (Button) findViewById(R.id.regR);
        back = (Button) findViewById(R.id.back_Login);




    }
}
